//
//  PrepareViewController.m
//  TestSchool
//
//  Created by wuxiaoquan on 12-11-2.
//  Copyright (c) 2012年 yaoxia. All rights reserved.
//

#import "PrepareViewController.h"
#import "UserManager.h"
#import "QuestionViewController.h"
#import "AppDelegate.h"

@interface PrepareViewController ()

@end

@implementation PrepareViewController

@synthesize countLabel,testId,questionList,startBtn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Instruction";
    [self joinTest];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.countLabel = nil;
    self.questionList = nil;
    self.testId = nil;
    self.testName = nil;
    self.startBtn = nil;
    
    request = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)beginAnswer:(id)sender
{
    NSLog(@"进入答题");
    QuestionViewController *detailController = [[QuestionViewController alloc]initWithNibName:@"QuestionViewController" bundle:nil];
    detailController.testName = self.testName;
    detailController.questionList = self.questionList;
    detailController.testId = self.testId;
    [self.navigationController pushViewController:detailController animated:YES];
}

-(void)joinTest
{
    [self showWaitView:nil];
    
    
    //NSString * URL_LOGIN = @"http://218.24.94.212/Test/jointest.jsp";
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSString * URL_LOGIN = [NSString stringWithFormat:@"%@/Test/jointest.jsp",delegate.baseUrl];
    
    NSURL *url = [NSURL URLWithString:URL_LOGIN];
    request = [ASIFormDataRequest requestWithURL:url];
    
    [request setPostValue:self.testId forKey:@"testid"];
    [request setPostValue:[UserManager getCurrentUser].userId forKey:@"userid"];
    
    [request setCompletionBlock:^{
        [self hideWaitView];
        // Use when fetching text data
        NSString *responseString = [request responseString];
        
        //NSLog(@"responseString = %@",responseString);//显示收到的信息
        
        id json = [responseString JSONValue];
        //NSLog(@"meg = %@",[json objectForKey:@"drugInfo"]);
        if ([[json objectForKey:@"code"]intValue] == 500) {
            UIAlertView * messageAlert = [[UIAlertView alloc]
                                          initWithTitle:@"Message"
                                          message:[NSString stringWithFormat:@"%@",[json objectForKey:@"message"]]
                                          delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
            [messageAlert show];
            return ;
        }
        self.startBtn.enabled = YES;
        self.questionList = [json objectForKey:@"questionlist"];
        self.countLabel.text = [NSString stringWithFormat:@"%d",self.questionList.count];
        NSLog(@"共有%d个题目",self.questionList.count);
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        [self hideWaitView];
        NSError *error = [request error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Network error"
                              message:@"Please check your network"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }];
    [request startAsynchronous];
}

@end
